<?php
namespace app\index\controller;
use think\Controller;
use think\Session;
use think\Db;
use think\Request;
class Phone extends Controller
{
	private $redis;
	protected function _initialize()
	{
		$this->redis = new \Redis();
        $this->redis->connect('127.0.0.1', 6379);

	}
	public function index()
	{
		$position = Db::name('position')
			->field('position_id,position_name,position_companyid,position_place,position_num,position_start,position_end,position_publish,position_require,position_payway')
			->select();
		$this->assign('list',$position);
		return $this->fetch('phone/index');
	}

	//显示登录
	public function showLogin()
	{
		$rand = mt_rand(100,999);
    	$csrf_token  = password_hash($rand,PASSWORD_DEFAULT);
    	// $this->redis->connect('127.0.0.1', 6379);
    	$this->redis->set('csrftoken', $csrf_token);
    	$this->assign('token',$csrf_token);
		return $this->fetch('phone/login');
	}

	public function login()
	{
    		$data = input('post.');
    		// dump($data);die;
    		if($this->redis->get('csrftoken')===$data['token']){
    			//查询数据库
    			$res = Db::name('user')->field('user_pwd')->where(["user_tel"=>$data['username'],'user_status'=>1])->find();
    			if($res){
    				//查到了用户 比较密码
					if (password_verify($data['pwd'],$res['user_pwd'])) { 
                        Session::set('tel',$data['username'].'用户');
                        Session::set('usercate',1);  
                        return $this->redirect('index/phone/index');              
					} else{
                        // $arr = ['code'=>0,'msg'=>'用户名或密码错误'];
                        $this->error('用户名或密码错误','index/phone/showLogin');
                    }
    			}else{   				
    				 $this->error('登陆失败','index/phone/showLogin');
    			}
    		}
    	return json($arr);
	}
	public function content($id)
	{
		$position = Db::name('position')
					    ->field('position_id,position_cateid,position_tel,position_apply,position_id,position_name,position_companyid,position_place,position_num,position_start,position_end,position_publish,position_require,position_payway')
					    ->find($id);
		$data = explode(',',$position['position_cateid']);
        $type = Db::name('category')->field('name')->find($data[1]);
        $place = Db::name('category')->field('name')->find($data[2]);
		$this->assign('place',$place['name']);
		$this->assign('list',$position);
		return $this->fetch('phone/content');
	}

	//报名
	public function showApply($pid)
	{
        $this->assign('pid',$pid);
		return $this->fetch('phone/apply');
	}

	public function apply($pid)
	{
		$data = input('post.');
        $usertel = Session::get('tel');
        $usertel = substr($usertel,0,11);
        $uid = Db::name('user')->field('user_id')->where('user_tel',$usertel)->find();

        $cid = Db::name('position')->field('position_companyid')->where('position_id',$pid)->find();
        $insert['uid'] = $uid['user_id'];
        $insert['pid'] = $pid; 
        $insert['cid'] = $cid['position_companyid'];
        $insert['tel'] = $data['tel'];
        $insert['name'] = $data['name'];
        $insert['address'] = $data['address'];
        $insert['addtime'] = time();
        if(Db::name('apply')->where(['uid'=>$uid['user_id'],'pid'=>$pid])->find()){
             // echo '<script>alert("您已经报名了该职位,请到个人信息查看");</script>';
              echo "<script> alert('您已经报名了该职位,请到个人信息查看');parent.location.href='http://www.zzz.com/index/phone/index'; </script>"; 
        }else{
            if(Db::name('apply')->insert($insert)){
                // echo '<script>alert("报名成功");</script>';
                  echo "<script> alert('报名成功');parent.location.href='http://www.zzz.com/index/phone/index'; </script>"; 
            }else{
                // echo '<script>alert("报名失败,请稍后再试...");</script>';
                echo "<script> alert('报名失败,请稍后再试...');parent.location.href='http://www.zzz.com/index/phone/index'; </script>"; 
            }
        }
	}
}